package com.gs.domain.wrapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.Observable;
import java.util.Vector;

import com.gs.business.DBUtil;
import com.gs.domain.IRisparmio;
import com.gs.domain.Risparmio;
import com.molinari.jdbcandroid.wrap.ConnectionAndroid;
import comi.gsi.command.javabeancommand.AbstractOggettoEntita;
import comi.gsi.command.javabeancommand.IWrapperEntity;

public class WrapRisparmio extends Observable implements IWrapperEntity, IRisparmio {

	private Risparmio risparmio;

	public WrapRisparmio() {
		risparmio = new Risparmio();
	}

	@Override
	public Object selectById(final int id) {
		Connection cn = DBUtil.getConnection();
		String sql = "SELECT * FROM " + Risparmio.NOME_TABELLA + " WHERE " + Risparmio.ID + "=" + id;
		Risparmio risparmio = null;
		try {

			Statement st = cn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if (rs.next()) {
				risparmio = new Risparmio();
				risparmio.setidRisparmio(rs.getInt(1));
				risparmio.setPerSulTotale(rs.getDouble(2));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.chiudiElementiDb((ConnectionAndroid) cn, null);
		}
		return risparmio;
	}

	@Override
	public Iterator<Object> selectWhere(final String where) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Vector<Object> selectAll() {
		Vector<Object> risparmi = new Vector<Object>();
		Connection cn = DBUtil.getConnection();
		String sql = "SELECT * FROM " + Risparmio.NOME_TABELLA;
		try {
			Statement st = cn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				Risparmio risparmio = new Risparmio();
				risparmio.setidRisparmio(rs.getInt(1));
				risparmio.setPerSulTotale(rs.getDouble(2));
				risparmi.add(risparmio);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return risparmi;

	}

	@Override
	public boolean insert(final Object oggettoEntita) {
		boolean ok = false;
		Connection cn = DBUtil.getConnection();
		String sql = "";
		try {
			Risparmio risparmio = (Risparmio) oggettoEntita;

			sql = "INSERT INTO " + Risparmio.NOME_TABELLA + " (" + Risparmio.PERCSULTOT + ") VALUES(?)";
			PreparedStatement ps = cn.prepareStatement(sql);

			ps.setDouble(1, risparmio.getPerSulTotale());
			ps.executeUpdate();
			ok = true;
		} catch (Exception e) {
			ok = false;
			e.printStackTrace();
		} finally {
			try {
				cn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		return ok;
	}

	@Override
	public boolean delete(final int id) {
		boolean ok = false;
		String sql = "DELETE FROM " + Risparmio.NOME_TABELLA + " WHERE " + Risparmio.ID + " = " + id;
		Connection cn = DBUtil.getConnection();

		try {
			Statement st = cn.createStatement();
			st.executeUpdate(sql);
			ok = true;

		} catch (SQLException e) {
			e.printStackTrace();
			ok = false;
		}
		try {
			cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return ok;
	}

	@Override
	public boolean update(final Object oggettoEntita) {
		boolean ok = false;
		Connection cn = DBUtil.getConnection();

		Risparmio risparmio = (Risparmio) oggettoEntita;
		String sql = "UPDATE " + Risparmio.NOME_TABELLA + " SET " + Risparmio.PERCSULTOT + " = " + risparmio.getPerSulTotale() + " WHERE " + Risparmio.ID + " = " + risparmio.getidRisparmio();
		try {
			Statement st = cn.createStatement();
			st.executeUpdate(sql);
			ok = true;

		} catch (SQLException e) {
			e.printStackTrace();
			ok = false;
		}
		try {
			cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return ok;
	}

	@Override
	public boolean deleteAll() {
		boolean ok = false;
		String sql = "DELETE FROM " + Risparmio.NOME_TABELLA;
		Connection cn = DBUtil.getConnection();

		try {
			Statement st = cn.createStatement();
			st.executeUpdate(sql);
			ok = true;

		} catch (SQLException e) {
			e.printStackTrace();
			ok = false;
		}
		try {
			cn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return ok;
	}

	@Override
	public AbstractOggettoEntita getEntitaPadre() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getidRisparmio() {
		return risparmio.getidRisparmio();
	}

	@Override
	public void setidRisparmio(final int idRisparmio) {
		risparmio.setidRisparmio(idRisparmio);
	}

	@Override
	public double getPerSulTotale() {
		return risparmio.getPerSulTotale();
	}

	@Override
	public void setPerSulTotale(final double PerSulTotale) {
		risparmio.setPerSulTotale(PerSulTotale);
	}

	@Override
	public String getNomeEntita() {
		return risparmio.getNomeEntita();
	}

	@Override
	public void notifyObservers() {
		super.notifyObservers();
	}

	@Override
	protected synchronized void setChanged() {
		super.setChanged();
	}

	@Override
	public AbstractOggettoEntita getEntita() {
		return this.getEntitaPadre();
	}

}
